In [1]:
from IPython.display import display, Markdown;
In [2]:
def print_code(path, lang=""):
with open(path) as f:
return Markdown("```%s\n%s```" % (lang,f.read()));
Taken from Scipy: Interfacing with C.
The Python-C-API is the backbone of the standard Python interpreter. Using this module it is possible to write Python extension modules in C and C++. Using this module often requires significant boilerplate code to convert between datatypes.
Using the Python-C-API is not recommended in practice, but it is useful to understand because other methods rely on the concepts in this section.
Advantages
Disadvantages
In [3]:
print_code("python-c-api/cos_module.c", "c")
Out[3]:
The following setup.py
file uses distutils
to compile the C extension.
In [4]:
print_code("python-c-api/setup.py", "python")
Out[4]:
We now call setup.py
with the following options:
build_ext
builds extension modules--inplace
will output the compiled extension module into the current directory
In [12]:
%%bash
cd python-c-api
python setup.py build_ext --inplace
Now we see that a new .so
file has been created:
In [15]:
%%bash
ls
In [17]:
import cos_module.cpython-35m-x86_64-linux-gnu as cos_module
In [ ]:
In [ ]: